package f.admin.user.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebFilter(filterName = "AdminFilter", urlPatterns = {"/admin/*"})
public class AdminFilter implements Filter {

	public void destroy() {
		
	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		String requestURI = request.getRequestURI();
		String targetURL = requestURI.substring(requestURI.indexOf("/", 1), requestURI.length());
		if(targetURL.equals("/admin/") || targetURL.equals("/admin/login.jsp")) {
			chain.doFilter(req, resp);
			return;
		}
		if(request.getSession().getAttribute("loginStatus") != null && Integer.parseInt(request.getSession().getAttribute("loginStatus").toString()) == 1) {
			chain.doFilter(req, resp);
		} else {
			response.sendRedirect(request.getContextPath() + "/admin/");
		}
	}

	public void init(FilterConfig config) throws ServletException {
		
	}

}
